<?php
/**
 * Created by PhpStorm.
 * User: wumengmeng <wu_mengmeng@foxmail.com>
 * Date: 2019/4/26 0026
 * Time: 16:54
 */

namespace Modules\Shop\Http\Middleware;

use Closure;
use HiCommon\Service\AuthService;
use Illuminate\Http\Request;
use Modules\Admin\Service\AdminsService;

class ShopAuth
{

    /**
     * 验证权限
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure                 $next
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function handle(Request $request, Closure $next)
    {
        //验证权限
        $sPermissionRouteAlias = $request->route()->getName();//路由别名
        $n_uid = shop_user_id();
        $is_master = shop_user()->is_master;
        $res = AuthService::api_shop_authority_auth($sPermissionRouteAlias,$n_uid,$is_master);

        if($res['state'] != 0){
           msg_error($res['msg']);
//            $this->messge(false, $res['msg'], route('admin_index'), 0);
        }
        return $next($request);
    }

    /**
     * 发送错误消息
     *
     * @param int    $status
     * @param string $message
     * @param string $redirect
     * @param int    $time
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    protected function messge(int $status, string $message, string $redirect = '', int $time = 3) {
        $data = [
          'status' => $status,
          'message' => $message,
          'redirect' => $redirect,
          'time' => $time,
        ];
        response(view('shop::public.messge', $data))->send();
        exit;
    }
}